/**
 * @date 11:48:35 AM
 * EducationTeacherMessageDaoImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.bean.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.qs.education.bean.dao.IEducationTeacherMessageDao;
import com.qs.education.bean.entity.EducationTeacher;
import com.qs.education.bean.entity.EducationTeacherMessage;
/**
 * @class EducationTeacherMessageDaoImpl	
 * @author Warren
 * @description
 * @date Nov 19, 2018 
 */
@Repository
public class EducationTeacherMessageDaoImpl extends BaseDaoImpl<EducationTeacherMessage> implements IEducationTeacherMessageDao{
	
	/**
	 * @author Warren
	 * @param EducationTeacher
	 * @return List<EducationMessage>
	 * @description 根据teacher查询message
	 * @date Nov 12, 2018
	 */
	@Override
	public List<EducationTeacherMessage> findListByTeacher(EducationTeacher educationTeacher,int currentPage,int pageSize) {
		String jql = "from EducationTeacherMessage etm where etm.teacher = :teacher";
		Map<String, Object> params = new HashMap<>();
		params.put("teacher", educationTeacher);
		if( educationTeacher.getGroup()!=null && !educationTeacher.getGroup().isEmpty()) {
			params.put("groups", educationTeacher.getGroup());
			jql = jql + " or etm.group in :groups order by id desc";
		}else {
			jql = jql + " order by id desc";
		}
		int firstResult = (currentPage-1)*pageSize;
		List<EducationTeacherMessage> result = query(jql, EducationTeacherMessage.class, firstResult, pageSize, params);
		if(null != result && !result.isEmpty()){
			return result;
		}
		return null;
	}

	/**
	 * @author Warren
	 * @param educationTeacher
	 * @return int
	 * @description 根据teacher查询message总条数
	 * @date Nov 21, 2018
	 */
	@Override
	public int getALLCountByTeacher(EducationTeacher educationTeacher) {
		/*String jql = "Select count(*) from EducationTeacherMessage etm where etm.teacher = ?1";
		Query query = em.createQuery(jql);
		query.setParameter(1, educationTeacher);
		if(educationTeacher.getGroups()!=null && !educationTeacher.getGroups().isEmpty()) {
			List<EducationGroup> groups = 
		}
		*/
		String jql = " Select count(*) from EducationTeacherMessage etm where etm.teacher = :teacher";
		Map<String, Object> params = new HashMap<>();
		params.put("teacher", educationTeacher);
		if( educationTeacher.getGroup()!=null && !educationTeacher.getGroup().isEmpty()) {
			params.put("groups", educationTeacher.getGroup());
			jql = jql + " or etm.group in :groups";
		}
		List<?> result = query(jql,params);
		if(null!=result && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}
		return 0;
	}

	/**
	 * @author Warren
	 * @return EducationTeacherMessage
	 * @description 
	 * @date Nov 23, 2018
	 */
	@Override
	public EducationTeacherMessage findEducationTeacherMessageByApplyIdAndStatus(Long applyId, int messageStatus) {
		String jql = "from EducationTeacherMessage etm where etm.applyId = :applyId and messageStatus = :messageStatus";
		Map<String, Object> params = new HashMap<>();
		params.put("applyId", applyId);
		params.put("messageStatus", messageStatus);
		List<EducationTeacherMessage> result = query(jql, EducationTeacherMessage.class, params);
		if(null != result && !result.isEmpty()){
			return result.get(0);
		}
		return null;
	}
	
}
